/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: Mt5GroupsServiceImpl
 * Author:   wl
 * Date:     2018/3/21 16:19
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.example.mam.service.impl;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.example.mam.constant.Constant;
import com.example.mam.dto.TreeNode;
import com.example.mam.entity.Mt5Groups;
import com.example.mam.exception.BizException;
import com.example.mam.exception.BizExceptionEnum;
import com.example.mam.mapper.Mt5GroupsMapper;
import com.example.mam.service.Mt5GroupsService;
import com.example.mam.util.ManagerAPIUtils;
import com.sicerora.mt5managerapi64j.base.IMTConGroup;
import com.sicerora.mt5managerapi64j.entity.OutInt;
import com.sicerora.mt5managerapi64j.factory.ManagerAPIFactory;
import com.sicerora.mt5managerapi64j.manager.IMTAdminAPI;
import com.sicerora.mt5managerapi64j.manager.IMTManagerAPI;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
public class Mt5GroupsServiceImpl implements Mt5GroupsService {

    @Resource
    private Mt5GroupsMapper mt5GroupsMapper;

    @Override
    public TreeNode selectAllGroupName() {
        List<Map> list = mt5GroupsMapper.selectAllGroup();
        TreeNode treeNode = new TreeNode();
        treeNode.setLabel("group");
        treeNode.setId("_");
        List<String[]> paths = new ArrayList<>();
        for (Map map : list) {
            String tmp = (String) map.get("groupName");
            paths.add(tmp.split("\\\\"));
        }
        generateTree(paths, treeNode, 0);
        return treeNode;
    }


    private static void generateTree(List<String[]> paths, TreeNode node, int layer) {
        // 将组整理成路径
        Set<TreeNode> children = new HashSet<>();
        for (String[] path : paths) {
            if (path.length - 1 > layer) {
                if (layer == 0 || node.getLabel().equals(path[layer - 1])) {
                    TreeNode treeNode = new TreeNode();
                    treeNode.setLabel(path[layer]);
                    StringBuilder stringBuilder = new StringBuilder();
                    for (int i = 0; i <= layer; i++) {
                        stringBuilder.append(path[i]);
                        stringBuilder.append("_");
                    }
                    treeNode.setId(stringBuilder.toString());
                    generateTree(paths, treeNode, layer + 1);
                    children.add(treeNode);
                }
            }
        }
        if (children.size() != 0) {
            node.setChildren(children);
        }

    }


    @Override
    public List selectAllMAMGroups() {
        List list = mt5GroupsMapper.selectAllMAMGroup();
        if (!ObjectUtils.isEmpty(list) && !ObjectUtils.isEmpty(list.get(0))) {
            Map map = (Map) list.get(0);

        }
        return list;
    }

    @Override
    public List selectAllAccountByGroups(Long id) {
        System.out.println(id);
        List  list = mt5GroupsMapper.selectAllAccountByMAMGroup(id);
        return list;
    }

    @Override
    public Integer updateGroup(Mt5Groups group) {

        return 1;
    }


    @Override
    public Integer setMAMGroup(Long id, String isMAM) {
        Mt5Groups group = mt5GroupsMapper.selectByPrimaryKey(id);

        if (!ObjectUtils.isEmpty(group) && group.getGroupId() != 0) {

            ManagerAPIFactory managerAPIFactory = ManagerAPIUtils.getManagerAPIFactory("D:/MT5APIManager64/");
            OutInt version = ManagerAPIUtils.getVersion(managerAPIFactory);
            IMTManagerAPI imtManagerAPI = ManagerAPIUtils.getIMTManagerAPI(managerAPIFactory, version, "D:/apispace/managerbbb");
            IMTAdminAPI imtAdminAPI = ManagerAPIUtils.getIMTAdminAPI(managerAPIFactory, version, "D:/apispace/admin");

            imtManagerAPI.connect("39.106.111.230:1990", 1002, "wl123456",
                    null, 0xffffffff, 30000);

            imtAdminAPI.connect("123.56.218.116:443", 1002, "wl123456",
                    null, 0, 30000);

            // 创建组对象
            IMTConGroup imtConGroup = new IMTConGroup(imtManagerAPI.groupCreate());
            // 根据组对象获取组
            int retCode = imtManagerAPI.groupRequest(group.getGroup(), imtConGroup);
            System.out.println(retCode);
            // 更改组对象中的公司邮箱信息
            imtConGroup.companyEmail(isMAM);

            // 提交组对象更改组信息
            retCode = imtAdminAPI.groupUpdate(imtConGroup);

            System.out.println(retCode);
            // 释放组对象
            imtConGroup.release();
            imtManagerAPI.disconnect();
            imtAdminAPI.disconnect();
            imtManagerAPI.release();
            imtAdminAPI.release();
            System.out.println("sssssssssssssssssssssssssssssssssssssss");
            return retCode;
        }
        return 0;
    }

    @Override
    public Integer cancleMAMGroup(Long id,String notMAM) {
        return setMAMGroup(id,notMAM);
    }

    @Override
    public List selectGroupById(String groupId) {
        System.out.println(groupId);
        System.out.println( groupId.concat("%").replaceAll("_","\\\\"));
        return mt5GroupsMapper.selectGroupById(groupId,"_*".equals(groupId) ? groupId : groupId.concat("%").replaceAll("_","\\\\\\\\"));
    }






}
